//
//  LLStatusCell.swift
//  WeiBoShiBing
//
//  Created by Bingo on 15/10/31.
//  Copyright © 2015年 Bingo. All rights reserved.
//

import UIKit
//自定义cell
class LLStatusCell: UITableViewCell {

    
    
    var CellDelegate:LLStatusCellDelegate?
    
    var pictureViewWidthCons:NSLayoutConstraint?
    var pictureViewHeightCons:NSLayoutConstraint?
    
    var status:LLStatus?{
        didSet{
            topView.status = status
            
            
            pictureView.status = status
            
            let size = pictureView.calcViewSize()
            
            pictureViewWidthCons?.constant = size.width
            
            pictureViewHeightCons?.constant = size.height
            
            contentLabel.attributedText = status?.attrText
        }
        
    }
    
    func rowHeight(status:LLStatus) ->CGFloat{
        self.status = status
        
        layoutIfNeeded()
        
        let maxY = CGRectGetMaxY(bottomView.frame)
        
        return maxY
    }
    //构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //准备ui
     func prepareUI(){
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 添加约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        
        // 微博内容
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
        
        // 设置宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:UIScreen.mainScreen().bounds.width - 2 * 8))
        

        
        
//        topSeparatopView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 10))
////
//        topView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topSeparatopView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
//        
//        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
//        
//        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * 8))
//        
//        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
//        
//      let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: nil, offset: CGPoint(x: 0, y: 8))
//        
//      pictureViewWidthCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        
//      pictureViewHeightCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
        
         bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView , size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44),offset: CGPoint(x: -8, y: 8))
    
    }
    
    //懒加载
    private lazy var topView:LLStatusTopView = LLStatusTopView()
    
     lazy var bottomView:LLStatusButtonView = LLStatusButtonView()
    
    lazy var contentLabel:FFLabel =  {
        let label = FFLabel(fonsize: 16, textColoc: UIColor.blackColor())
        label.numberOfLines = 0
        
        label.labelDelegate = self
        return label
    }()
    lazy var pictureView:LLStatusPictureView = LLStatusPictureView()
}

extension LLStatusCell:FFLabelDelegate{

    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
        if text.hasPrefix("http"){
            CellDelegate?.urlClick(text)
        }
        
    }

}


protocol LLStatusCellDelegate:NSObjectProtocol{

    func urlClick(text: String)

}
